
//页面的类型常量及变量
var LOGIN="LOGIN_PAGE";
var READ="READER_PAGE";
var PAY="SUBSCRIBE_PAGE";
var DETAIL="DETAIL_PAGE";
var CHAPTER="CHAPTERLIST_PAGE";
var CLASS="CLASSPAGE_PAGE";
var RECENT_READ="RECENT_READING_PAGE";
var SEARCH="SEARCH_PAGE";
var vPLAT=LOGIN;

//页面数据分类
var SUCCESS=1;
var DELAY=2;
var vTimeCode=DELAY;

function getPLATType(){
	var thisURL = document.URL;   
	var value =thisURL.split('?')[1];  
	vPLAT= value.split('=')[1];  
	console.log("thisURL="+thisURL+",vPLAT="+vPLAT);		 
}

function onTime(e){
	var timeId= e.getAttribute("id");
	e.className="select-style";
	console.log("timeId="+timeId)
	var theOtherElement= "";
	if(timeId=="left-title"){
		vTimeCode=DELAY;
		theOtherElement=document.getElementById("right-title");
	}else if(timeId=="right-title"){
		vTimeCode=SUCCESS;
		theOtherElement=document.getElementById("left-title");
	}
	theOtherElement.className="unselect-style";
	console.log("vTimeCode="+vTimeCode)
	
	var title=e.innerHTML;
	console.log("title:"+title);
	var tHeadTitle= document.getElementById("average-title");
	tHeadTitle.innerHTML=title;		
	mRequest.send();
}

window.onload= function (){
    getPLATType();	
	mTitle.setTitleBar();
	//console.log(mRequest.requestUrl());
	mRequest.send();
	
	
	/*
	var myData = new Array(["360",173],["百度",158],["应用宝",150],["华为",239],["VOVO",63],["OPPO",239]);
	var colors=new Array();
	for(var i=0;i<myData.length;i++){
		colors[i]="#ff0000";
	}	
	var myChart = new JSChart('chart', 'bar');
	myChart.setDataArray(myData);
	myChart.colorizeBars(colors);
	myChart.setAxisColor('red');
	myChart.setAxisWidth(1);
	myChart.setAxisNameX('');
	myChart.setAxisNameY('');
	myChart.setAxisNameColor('#619CD2');
	myChart.setAxisNameFontSize(9);
	myChart.resize(331, 197);
	myChart.setGridColor('#5D5F5D');
	*/
	//myChart.draw();
}
/*****************数据请求及处理*******************/
var mRequest={
	requestUrl:function(){
		var url=addMethod(strUrlHost,"platformtimedelay");
		url=addToken(url);
		var itemcode = vPLAT;
		url=addItemCode(url,itemcode)
		var optType=vTimeCode;
		url=addOptionType(url,optType);
	    console.log("url:"+url);
		return url;
	},
	send:function(){
			sendRequest(this.requestUrl(), window.mData.process);
			console.log("send");		
	}
	
}
/******************数据处理及图表绘制*************************/
var mData={
	process:function (resultData){	
		var flag = resultData.success;	
		console.log("flag="+flag);		
		if(flag==1){
			var vData=resultData.data;			
			var type=vPLAT;
			switch(type){
			case "LOGIN_PAGE":
			case "READER_PAGE":
			case "SUBSCRIBE_PAGE":
			case "DETAIL_PAGE":
			case "CHAPTERLIST_PAGE":
			case "CLASSPAGE_PAGE":
			case "RECENT_READING_PAGE":
			case "SEARCH_PAGE":
			    console.log("--------"+type+"--------");
				mData.processPLATData(vData);
                break;								
			}
		}
	},
	processPLATData:function(vData){
		console.log(vData.length);		
		var charTime="";
		var Average=0;
		var timeSlice="";
		var chartDataArray= new Array();
		mHTML.clearRow();
		if(vData.length>0){
			for(var i=0;i<vData.length;i++){
			var temp=vData[i];
            var timeTemp=temp.time;	
            var charTimeTemp=timeTemp.slice(4,6)+'.'+timeTemp.slice(6,8);
			timeSlice=timeTemp.slice(6,8);
			charTime=timeTemp.slice(0,4)+'.'+charTimeTemp;			
		    if(vTimeCode==DELAY){
				Average=vData[i].rate;
			}else if(vTimeCode==SUCCESS){
				Average=vData[i].rate;
			} 
			console.log("charTime="+charTime+",Average="+Average);
			mHTML.addRow(charTime,Average);
			var j=vData.length-1-i;
			chartDataArray[j]=[timeSlice,Average];
			}
		}
		else{
			for(var j=0;j<20;j++){
			if(j<10){
				timeSlice="0"+j;
			}else{
				timeSlice=j;
			}
			charTime="2016.07."+timeSlice;
			Average=j/10;
			console.log("charTime="+charTime+",Average="+Average);
			mHTML.addRow(charTime,Average);
			var k=vData.length-1-j;
			chartDataArray[k]=[timeSlice,Average];	
			}
		}
		mHTML.drawChart(chartDataArray);
		mHTML.drawRow();
	}	
}
/************图表绘制**************************/
var mHTML={
	strRow:"",
	clearRow:function(){
		mHTML.strRow="";
	},
	addRow:function(charTime,Average){	
		mHTML.strRow+= "<tr><td>"+charTime+"</td><td>"+Average+"ms</td></tr>";
		console.log(mHTML.strRow);		
	},
	drawRow:function(){
		var tbodyElement=document.getElementById("tBody");
		tbodyElement.innerHTML=mHTML.strRow;
	},
	drawChart:function(dataArray){
		var myChart = new JSChart('chart', 'line');	
		myChart.setDataArray(dataArray);
		myChart.setLineColor('#63a9ea');
		myChart.setFlagColor('#63a9ea');
		for(var i=0;i<dataArray.length;i++){
			myChart.setTooltip([dataArray[i][0],"rate:"+dataArray[i][1]]);
		}
	    myChart.setTitle('');
	    myChart.setAxisNameX('');
	    myChart.setAxisNameY('');
	    myChart.setAxisPaddingTop(10);	
	    myChart.setAxisPaddingLeft(30);
	    myChart.resize(screen.width, 217);
	    myChart.setGridColor('#5D5F5D');
	    myChart.setGrid(true);
	    myChart.setShowXValues(false);
	    myChart.draw();	
	}
}



/************标题栏**************************/
var mTitle = {    
	setTitleBar:function(){		
			switch(window.vPLAT){
			case  LOGIN:
				this.setTitle("登录");
				break;
			case  READ:		
				this.setTitle("阅读");
				break;
			case  PAY:
				this.setTitle("订购");
				break;
			case  DETAIL:
				this.setTitle("详情");
				break;
			case  CHAPTER:;
				this.setTitle("章节列表");
				break;
			case  CLASS:	
				this.setTitle("分类页面");
				break;
			case  RECENT_READ:
				this.setTitle("最近阅读");
				break;
			case  SEARCH:
				this.setTitle("搜索页");
				break;
			default:					
				break;
		}
	},
	setTitle:function(title){
		var titleElement=document.getElementById("spanTitle");
		titleElement.innerHTML=title;		
	}
}


